﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DnPakExplorer {
	public interface IItem{

		#region 固定属性，不可变

		List<Property> BasePropertyList { get; }

		List<EnchantRecord> EnchantProperty { get; }

		List<PotencialRecord> PotentialProperty1 { get; }

		List<PotencialRecord> PotentialProperty2 { get; }

		int id { get ; }					// 物品ID

		string name { get ; }			// 名称

		int type { get ; }				// 物品类型

		int limitLevel { get ; }			// 等级限制

		int rank { get ; }				// 级别

		string needJobClassName { get ; }		// 职业类型

		string needJobClass { get ; }

		int iconId { get ; }			// 图标索引

		string description { get ; }		// 描述

		ItemType exchangType { get ; }		// 交易(物品)类型

		int typeParam1 { get ; }		// 制作属性类型

		int typeParam2 { get ; }		// 附魔属性类型

		int enchantId { get ; }		// 强化属性表

		int slotsType { get ; }		// 龙玉槽类型

		bool isCash { get ; }		// 是否点券物品

		int skillId { get ; }		// 技能ID

		string skillDecription { get ; }

		int setId { get ; }			// 套装ID

		FairyType fairyType { get; }

		SetEntry set { get ; set ; }

		List<int> slotTypes { get; }

		ImprintingType imprintingType { get; }

		#endregion
	}

	public interface IUserItem : IItem , IEnumerable<Property>{
		// 应用的制作属性
		PotencialRecord AppliedPotential1Property { get; }
		int appliedPotential1 { get; set; }

		// 应用的附魔属性
		PotencialRecord AppliedPotential2Property { get; }
		int appliedPotential2 { get; set; }

		// 应用的强化等级
		List<Property> AppliedEnchantProperty { get; }

		int appliedEnchantLevel { get; set; }

		// 应用的龙玉
		List<DragonJewelSlot> AppliedSlotsList { get; }// 镶嵌的龙玉

		List<Property> AppliedGrowProperty { get; }

		ImprintingSlot AppliedImprintingSlot { get; }

		int petMaxLevel { get; }

		int petLevelType { get; }

		int petLevel { get; set; }


		IUserItem Clone();
	}

	public class Item : IItem {

		

		public Item(Dntable itemTable, int row) {
			this.fromTable = itemTable;
			this.row = row;
		}

		#region 固定属性，不可变

		public virtual List<Property> BasePropertyList {
			get {
				if (_PropertyList == null) {
					_PropertyList = new List<Property>();
					for (int i=15; i<=51; i+=4) {
						Property prop = new Property();
						prop.id = (PropertyID)_fromTable[_row, i];
						if (prop.id < 0 || (float)_fromTable[_row, i+1]<0.000001f)
							break;
						string value = (string)_fromTable[_row, i+2];
						if (Util.StateVlauePettern.IsMatch(value)) {
							prop.value = float.Parse(value);
							if (prop.value == 0) {
								continue;
							}
						} else {
							break;
						}
						_PropertyList.Add(prop);
					}
				}
				return _PropertyList;
			}
		}
		private List<Property> _PropertyList;

		public virtual List<EnchantRecord> EnchantProperty {
			get {
				if (_EnchantProperty == null && enchantId > 0) {
					_EnchantProperty = new List<EnchantRecord>();
					foreach (Dntable table in DnResourceUtil.EnchantTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if ((int)table[r, 0] == enchantId) {
								_EnchantProperty.Add(new EnchantRecord(table, r));
							}
						}
					}
					_EnchantProperty.TrimExcess();
				}
				return _EnchantProperty;
			}
		}
		private List<EnchantRecord> _EnchantProperty;

		public virtual List<PotencialRecord> PotentialProperty1 {
			get {
				if (_PotentialProperty1 == null && typeParam1>0) {
					int potentialId = typeParam1;
					_PotentialProperty1 = new List<PotencialRecord>();
					foreach (Dntable table in DnResourceUtil.PotencialTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if ((int)table[r, 0] == potentialId) {
								_PotentialProperty1.Add(new PotencialRecord(table, r));
							}
						}
					}
					_PotentialProperty1.TrimExcess();
				}
				return _PotentialProperty1;
			}
		}
		private List<PotencialRecord> _PotentialProperty1;

		public virtual List<PotencialRecord> PotentialProperty2 {
			get {
				if (_PotentialProperty2 == null && typeParam2>0) {
					_PotentialProperty2 = new List<PotencialRecord>();
					int potentialId = typeParam2;
					foreach (Dntable table in DnResourceUtil.PotencialTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if ((int)table[r, 0] == potentialId) {
								_PotentialProperty2.Add(new PotencialRecord(table, r));
							}

						}
					}
					_PotentialProperty2.TrimExcess();
				}
				return _PotentialProperty2;
			}
		}
		private List<PotencialRecord> _PotentialProperty2;

		public virtual Dntable fromTable {
			get { return _fromTable; }
			private set { _fromTable = value; }
		}		// 所在表
		private Dntable _fromTable;

		public int row {
			get { return _row; }
			private set { _row = value; }
		}
		private int _row;

		public virtual int id {
			get {
				return fromTable.GetKey(_row);
			}
		}					// 物品ID

		public virtual string name {
			get {
				int id = (int)fromTable[_row, 0];
				if (id == 0) {
					return "";
				}
				string s = Util.FindUIString(id);
				string nameParams = (string)fromTable[_row, 2];
				int c;
				if (nameParams != "") {
					s = string.Format(s, Util.parseParams3(nameParams, out c));
				}
				return s;
			}
		}			// 名称

		public virtual int type {
			get { return (int)_fromTable[_row, 4]; }
		}				// 物品类型

		public virtual int limitLevel {
			get {
				return (int)fromTable[_row, 8];
			}
		}			// 等级限制

		public virtual int rank {
			get {
				return (int)fromTable[_row, 10];
			}
		}				// 级别

		public virtual string needJobClassName {
			get {
				string jobName = null;
				foreach (string jonstring in needJobClass.Split(';')) {
					if (jonstring == "")
						continue;
					string name = null;
					int job = int.Parse(jonstring);
					if (job <=0)
						name = "无限制";
					else {
						Dntable jobTable = DnResourceUtil.JobTable;
						int jobRow = -1;
						for (int i=0; i< jobTable.RecordCount; i++) {
							if (jobTable.GetKey(i) == job) {
								jobRow = i;
							}
						}
						if (jobRow < 0 || (name = Util.FindUIString((int)DnResourceUtil.JobTable[jobRow, 0])) == null) {
							name = job.ToString();
						}

					}
					if (name != null) {
						if (jobName == null) {
							jobName = name;
						} else {
							jobName += ";"+name;
						}
					}
				}
				return jobName;
			}
		}		// 职业类型

		public virtual string needJobClass {
			get {
				return (string)fromTable[_row, 9];
			}
		}

		public virtual int iconId {
			get {
				return (int)fromTable[_row, 12];
			}
		}			// 图标索引

		public virtual string description {
			get {
				int id = (int)fromTable[_row, 1];
				if (id == 0) {
					return "";
				}
				string s = Util.FindUIString(id);
				string nameParams = (string)fromTable[_row, 3];
				int c;
				if (nameParams != "") {
					s = string.Format(s, Util.parseParams3(nameParams, out c));
				}
				return s;
			}
		}		// 描述

		public virtual ItemType exchangType {
			get {
				return (ItemType)fromTable[_row, 84];
			}
		}		// 交易(物品)类型

		public virtual int typeParam1 {
			get {
				return (int)fromTable[_row, 5];
			}
		}		// 制作属性类型

		public virtual int typeParam2 {
			get {
				return (int)fromTable[_row, 6];
			}
		}		// 附魔属性类型

		public virtual int enchantId {
			get {
				return (int)fromTable[_row, 70];
			}
		}		// 强化属性表

		public virtual int slotsType {
			get {
				return (int)fromTable[_row, 90];
			}
		}		// 龙玉槽类型

		public virtual bool isCash {
			get { return (bool)fromTable[_row, 63]; }
		}		// 是否点券物品

		public virtual int skillId {
			get {
				return (int)fromTable[_row, 56];
			}
		}		// 技能ID

		public virtual string skillDecription {
			get {
				if (_skillDecription == null && skillId > 0) {
					if ((int)fromTable[_row, 58] == 2) {
						_skillDecription = DnResourceUtil.GetSkillDescription(this.skillId, 1);
					} else if ((int)fromTable[_row, 58] == (int)SkillUsingType.LevelUp) {
						_skillDecription = "#y"+DnResourceUtil.GetSkillName(skillId)+"+1";
					}

				}
				return _skillDecription;
			}
		}
		private string _skillDecription;

		public virtual FairyType fairyType {
			get {
				if (_fairyType == FairyType.Unkown) {
					Dntable table = DnResourceUtil.FairyTypeTable;
					int index = table.IndexOfField(0, id);
					_fairyType = index >= 0?(FairyType)(int)table[index, 1]:FairyType.NotAFairty;
				}
				return _fairyType;
			} 
		}
		private FairyType _fairyType = FairyType.Unkown;

		public virtual int setId {
			get {
				if (_setId == -1)
					return _setId;
				if (_set == null) {
					DnResourceUtil.GetSet(this);
					if (_set == null) {
						_setId = -1;
						return -1;
					} else
						_setId = set.setID;
				}
				return _setId;
			}
		}			// 套装ID
		private int _setId = -2;		// -2：未知，-1：没有

		public virtual SetEntry set {
			get {
				if (setId == -1)
					return null;
				return _set;
			}
			set {
				_set = value;
			}
		}
		private SetEntry _set;

		public ImprintingType imprintingType { 
			get {
				if (_impringType == ImprintingType.Unkown) {
					foreach (Dntable table in DnResourceUtil.ImprintingTables) {
						int id = this.id;
						for (int i=0; i<table.RecordCount; i++) {
							if (table.GetKey(i) == id) {
								return _impringType = (ImprintingType)(int)table[i, 0];
							}
						}
					}
					_impringType = ImprintingType.None;
				}
				return _impringType;
			} 
		}
		private ImprintingType _impringType = ImprintingType.Unkown;

		#endregion

		public virtual List<int> slotTypes {
			get {
				List<int> list = new List<int>();
				if (slotsType>0) {
					Dntable dragonJewelTable = DnResourceUtil.DranonJewelSlotsTable;
					for (int r=0; r<dragonJewelTable.RecordCount; r++) {
						if ((int)dragonJewelTable[r, 0] == slotsType) {
							for (int c=2; c<5; c++) {
								if ((int)dragonJewelTable[r, c]>0) {
									list.Add((int)dragonJewelTable[r, c]);
								}
							}
						}
					}
				}
				return list;
			}
		}// 镶嵌的龙玉

	}

	public class UserItem : Item , IUserItem{

		public UserItem(Dntable fromTable, int row):base(fromTable, row) {

		}

		public IUserItem Clone() {
			UserItem item = new UserItem(fromTable, row);
			item.appliedEnchantLevel = appliedEnchantLevel;
			item.appliedPotential1 = appliedPotential1;
			item.appliedPotential2 = appliedPotential2;
			item.petLevel = petLevel;
			List<DragonJewelSlot> djs = AppliedSlotsList;
			if (djs != null) {
				for (int i=0; i<djs.Count; i++) {
					if (djs[i].Item != null)
						item.AppliedSlotsList[i].Item = djs[i].Item.Clone();
				}
			}

			return item;
		}

		public virtual IEnumerator<Property> AllAppliedProperties() {
			List<Property> list = null;
			list = BasePropertyList;
			if (list != null && list.Count>0) {
				foreach (Property prop in list) {
					yield return prop;
				}
			}

			list = AppliedEnchantProperty;
			if (list != null && list.Count>0) {
				foreach (Property prop in list) {
					yield return prop;
				}
			}

			PotencialRecord potencail1 = AppliedPotential1Property;
			if (potencail1 != null && potencail1.Properties.Count>0) {
				foreach (Property prop in potencail1.Properties) {
					yield return prop;
				}
			}

			PotencialRecord potencail2 = AppliedPotential2Property;
			if (potencail2 != null && potencail2.Properties.Count>0) {
				foreach (Property prop in potencail2.Properties) {
					yield return prop;
				}
			}

			list = AppliedGrowProperty;
			if (list != null && list.Count>0) {
				foreach (Property prop in list) {
					yield return prop;
				}
			}

			ImprintingSlot imprintingSlot = AppliedImprintingSlot;
			if (imprintingSlot != null) {
				foreach (Property prop in imprintingSlot) {
					yield return prop;
				}
			}

			List<DragonJewelSlot> djList = AppliedSlotsList;
			if (djList != null && djList.Count>0) {
				foreach (DragonJewelSlot slot in djList) {
					if (slot.Item != null) {
						foreach (Property prop in slot.Item) {
							yield return prop;
						}
					}
				}
			}
		}

		public IEnumerator<Property> GetEnumerator() {
			return AllAppliedProperties();
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}

		#region 用户属性，可变
		// 应用的制作属性
		public virtual PotencialRecord AppliedPotential1Property {
			get {
				if (_AppliedPotential1 == null && appliedPotential1>0) {
					foreach (Dntable table in DnResourceUtil.PotencialTables) {
						int row = table.IndexOfKey(appliedPotential1);
						if (row >= 0) {
							_AppliedPotential1 = new PotencialRecord(table, row);
							break;
						}
					}
				}
				return _AppliedPotential1;
			}
		}
		private PotencialRecord _AppliedPotential1;

		// 应用的附魔属性
		public virtual PotencialRecord AppliedPotential2Property {
			get {
				if (_AppliedPotential2 == null && appliedPotential2>0) {
					foreach (Dntable table in DnResourceUtil.PotencialTables) {
						int row = table.IndexOfKey(appliedPotential2);
						if (row >= 0) {
							_AppliedPotential2 = new PotencialRecord(table, row);
							break;
						}
					}
				}
				return _AppliedPotential2;
			}
		}
		private PotencialRecord _AppliedPotential2;

		// 应用的强化等级
		public virtual List<Property> AppliedEnchantProperty {
			get {
				if (_AppliedEnchant == null && appliedEnchantLevel > 0) {
					_AppliedEnchant = new List<Property>();
					foreach (Dntable table in DnResourceUtil.EnchantTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if ((int)table[r, 0] == enchantId && (int)table[r, 1]==appliedEnchantLevel) {
								for (int i=17; i<=35; i++) {
									Property prop = new Property();
									prop.id = (PropertyID)table[r, i++];
									if (prop.id < 0)
										break;
									string value = (string)table[r, i];
									if (Util.StateVlauePettern.IsMatch(value)) {
										prop.value = float.Parse(value);
										if (prop.value == 0) {
											continue;
										}
									} else {
										break;
									}
									_AppliedEnchant.Add(prop);
								}
							}
						}
					}
				}
				return _AppliedEnchant;
			}
		}
		private List<Property> _AppliedEnchant;

		public List<Property> AppliedGrowProperty {
			get {
				if (_AppliedGrowProperty == null && petLevelType >= 0) {
					_AppliedGrowProperty = new List<Property>();
					int levelType = petLevelType;
					Dntable table = DnResourceUtil.PetLevelTable;
					for (int r=0; r<table.RecordCount; r++) {
						if ((int)table[r, 0] == levelType && (int)table[r, 1] == petLevel) {
							for(int c=3; c<30; c+=3){
								Property prop = new Property();
								prop.id = (PropertyID)(int)table[r, c];
								if(prop.id < 0 || !Util.StateVlauePettern.IsMatch((string)table[r, c+1]))
									break;
								prop.value = float.Parse((string)table[r, c+1]);
								if (prop.value == 0) {
									continue;
								}
								_AppliedGrowProperty.Add(prop);
							}
						}
					}
				}
				return _AppliedGrowProperty;
			}
		}
		private List<Property> _AppliedGrowProperty;

		public ImprintingSlot AppliedImprintingSlot {
			get {
				if (_AppliedImprintingItem == null && imprintingType != ImprintingType.None) {
					_AppliedImprintingItem = new ImprintingSlot(imprintingType);
				}
				return _AppliedImprintingItem;
			}
		}
		private ImprintingSlot _AppliedImprintingItem;

		public int appliedEnchantLevel {
			get { return _appliedEnchantLevel; }
			set {
				if (_appliedEnchantLevel != value)
					_AppliedEnchant = null;
				_appliedEnchantLevel = value;
			}
		}
		private int _appliedEnchantLevel = 0;

		public int appliedPotential1 {
			get { return _appliedPotential1; }
			set {
				if (_appliedPotential1 != value) {
					_AppliedPotential1 = null;
				}
				_appliedPotential1 = value;
			}
		}
		private int _appliedPotential1 = -1;

		public int appliedPotential2 {
			get { return _appliedPotential2; }
			set {
				if (_appliedPotential2 != value) {
					_AppliedPotential2 = null;
				}
				_appliedPotential2 = value;
			}
		}
		private int _appliedPotential2 = -1;

		// 应用的龙玉
		public virtual List<DragonJewelSlot> AppliedSlotsList {
			get {
				if (_slotList == null && slotsType>0) {
					_slotList = new List<DragonJewelSlot>();
					Dntable dragonJewelTable = DnResourceUtil.DranonJewelSlotsTable;
					for (int r=0; r<dragonJewelTable.RecordCount; r++) {
						if ((int)dragonJewelTable[r, 0] == slotsType) {
							for (int c=2; c<=5; c++) {
								if ((int)dragonJewelTable[r, c]>0) {
									_slotList.Add(new DragonJewelSlot((int)dragonJewelTable[r, c]));
								}
							}
						}
					}
				}
				return _slotList;
			}
		}// 镶嵌的龙玉
		List<DragonJewelSlot> _slotList;

		public int petLevelType {
			get {
				if (vehicleRow >= 0) {
					return (int)DnResourceUtil.VehicleTable[vehicleRow, 7];
				}
				return -1;
			}
		}

		private int vehicleRow {
			get {
				if (_vehicleRow == -2) {
					Dntable table = DnResourceUtil.VehicleTable;
					for (int r=0; r<table.RecordCount; r++) {
						if (table.GetKey(r) == id) {
							_vehicleRow = r;
							return _vehicleRow;
						}
					}
					_vehicleRow = -1;
				}
				return _vehicleRow;
			}
		}
		private int _vehicleRow = -2;

		public int petLevel {
			get { return _petLevel; }
			set { 
				if (_petLevel != value) {
					_AppliedGrowProperty = null;
				}
				_petLevel = value;
			}
		}
		private int _petLevel = 0;

		public int petMaxLevel {
			get {
				int maxLevel = 0;
				if (petLevelType >= 0) {
					int levelType = petLevelType;
					Dntable table = DnResourceUtil.PetLevelTable;
					for (int r=0; r<table.RecordCount; r++) {
						if ((int)table[r, 0] == levelType) {
							if(maxLevel < (int)table[r, 1]){
								maxLevel = (int)table[r, 1];
							}
						}
					}
				}
				return maxLevel;
			}
		}

		#endregion
	}

	public class EnchantRecord {

		private Dntable mTable;
		private int mRow;
		private List<Property> mProperties;

		public EnchantRecord(Dntable table, int row) {
			mTable = table;
			mRow = row;
		}

		public List<Property> Properties {
			get {
				if (mProperties == null) {
					mProperties = new List<Property>();
					for (int i=17; i<=35; i++) {
						Property prop = new Property();
						prop.id = (PropertyID)mTable[mRow, i++];
						if (prop.id < 0)
							break;
						string value = (string)mTable[mRow, i];
						if (Util.StateVlauePettern.IsMatch(value)) {
							prop.value = float.Parse(value);
							if (prop.value == 0) {
								continue;
							}
						} else {
							break;
						}
						mProperties.Add(prop);
					}
					mProperties.TrimExcess();
				}
				return mProperties;
			}
		}

		public int Level {
			get { return (int)mTable[mRow, 1]; }
		}

		public float SuccessRo {
			get { return (float)mTable[mRow, 2]; }
		}

		public float BreakRo {
			get { return (float)mTable[mRow, 3]; }
		}

		public int MinDown {
			get { return (int)mTable[mRow, 4]; }
		}

		public int MaxDown {
			get { return (int)mTable[mRow, 5]; }
		}
	}

	public class PotencialRecord {
		private Dntable mTable;
		private int mRow;
		private List<Property> mProperties;

		public PotencialRecord(Dntable table, int row) {
			mTable = table;
			mRow = row;
		}

		public int Key {
			get { return mTable.GetKey(mRow); }
		}

		public List<Property> Properties {
			get {
				if (mProperties == null) {
					mProperties = new List<Property>();
					for (int i=4; i<34; i++) {
						Property prop = new Property();
						prop.id = (PropertyID)mTable[mRow, i++];
						if (prop.id < 0)
							break;
						string value = (string)mTable[mRow, i];
						if (Util.StateVlauePettern.IsMatch(value)) {
							prop.value = float.Parse(value);
							if (prop.value == 0) {
								continue;
							}
						} else {
							break;
						}
						mProperties.Add(prop);
					}
					mProperties.TrimExcess();
				}
				return mProperties;
			}
		}

		public Dntable Table {
			get { return mTable; }
		}

		public int Row {
			get { return mRow; }
		}

		public float Redio {
			get { return (float)mTable[mRow, 2]; }
		}

		public int SkillId {
			get { return (int)mTable[mRow, 36]; }
		}

		public int SkillLevel {
			get { return (int)mTable[mRow, 37]; }
		}

		public int SkillUsingType {
			get { return (int)mTable[mRow, 38]; }
		}
	}

	public enum ImprintingType : sbyte{
		Unkown = -2,
		None = 0,
		Weapon = 1,
		Armor = 2,
		Suit = 4
	}

	public enum SkillUsingType { 
		Drug = 2,
		// 3 永久性增加属性
		LevelUp = 4,
		NewSkill = 5,
	}

	public class DragonJewelSlot : IItemSlot{

		public event ItemSlotItemChangedHandlder OnItemChanged;

		private IUserItem mItem;
		private int mType;

		public DragonJewelSlot(int type) {
			mType = type;
		}

		public int DragonJewelSlotType {
			get { return mType; }
		}

		public object Tag { get; set; }

		public IUserItem Item {
			get { return mItem; }
			set {
				IUserItem old = mItem;
				mItem = value;
				if (old != value && OnItemChanged!=null) {
					OnItemChanged(this, old);
				}
			}
		}

		public virtual bool CanEquip(IUserItem item) {
			return (item.exchangType == ItemType.DragonJewel || item.exchangType == ItemType.SkillDragonJewel)
				&& DnResourceUtil.GetDragonJewelType(item.id) == (DragonJewelType)mType;
		}

		public IUserItem Unequip() {
			IUserItem oldItem = Item;
			Item = null;
			return oldItem;
		}

		public virtual IUserItem[] Equip(IUserItem item) {
			IUserItem oldItem = Item;
			if (CanEquip(item)) {
				Item = item;
			}
			if (oldItem != null) {
				return new IUserItem[] { oldItem };
			}
			return new IUserItem[] { };
		}


		public int Id {
			get { return mType; }
		}

		public string SlotName {
			get { return "龙玉槽，类型："+mType.ToString(); }
		}

		public virtual IEnumerator<Property> GetEnumerator() {
			if (Item != null)
				return Item.GetEnumerator();
			return EmptyEnumerator();
		}

		protected IEnumerator<Property> EmptyEnumerator() {
			yield break;
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}


		public void CleanupEvents() {
			OnItemChanged = null;
		}
	}

	public class ImprintingSlot : IItemSlot {

		public event ItemSlotItemChangedHandlder OnItemChanged;

		private IUserItem mItem;
		private ImprintingType mType;

		public ImprintingSlot(ImprintingType type) {
			mType = type;
		}

		public ImprintingType ImprintingType {
			get { return mType; }
		}

		public object Tag { get; set; }

		public IUserItem Item {
			get { return mItem; }
			set {
				IUserItem old = mItem;
				mItem = value;
				if (old != value && OnItemChanged!=null) {
					OnItemChanged(this, old);
				}
			}
		}

		public virtual bool CanEquip(IUserItem item) {
			return item.exchangType == ItemType.Imprinting && (int)mType == item.typeParam2;
		}

		public IUserItem Unequip() {
			IUserItem oldItem = Item;
			Item = null;
			return oldItem;
		}

		public virtual IUserItem[] Equip(IUserItem item) {
			IUserItem oldItem = Item;
			if (CanEquip(item)) {
				Item = item;
			}
			if (oldItem != null) {
				return new IUserItem[] { oldItem };
			}
			return new IUserItem[] { };
		}


		public int Id {
			get { return 0; }
		}

		public string SlotName {
			get { return "刻印槽，类型："+mType.ToString(); }
		}

		public virtual IEnumerator<Property> GetEnumerator() {
			if (Item != null)
				return Item.GetEnumerator();
			return EmptyEnumerator();
		}

		protected IEnumerator<Property> EmptyEnumerator() {
			yield break;
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}


		public void CleanupEvents() {
			OnItemChanged = null;
		}
	}

	public class SetEntry {
		public static Regex mStateVlauePettern = new Regex(@"\d*\.?\d+$");

		public int setID;
		public List<IItem> setItems;
		public List<Property> properties {
			get {
				if (_properties == null) {
					_properties = new List<Property>();
					foreach (Dntable table in DnResourceUtil.SetTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if (table.GetKey(r) == setID) {
								for (int i=0; i<=33; i++) {
									if ((int)table[r, 0] <=0 /*|| (int)table[r+1, 0]<0*/)
										break;
									Property prop = new Property();
									prop.tag = (int)table[r, i++];
									prop.id = (PropertyID)(int)table[r, i++];
									string value = (string)table[r, i];
									if (mStateVlauePettern.IsMatch(value)) {
										prop.value = float.Parse(value);
									} else {
										prop.value = float.NaN;
										break;
									}
									_properties.Add(prop);
								}
								break;
							}
						}
					}
				}
				return _properties;
			}
		}
		private List<Property> _properties;
		public string setDescription;
	}

	public enum FairyType : int {
		Unkown  = -3,
		NotAFairty = -2,
		None	= -1,
		Type1	= 1,
		Type2	= 2,
		Type3	= 3,
	}

	public enum DragonJewelType : int {
		None = 0,
		Attack = 1,
		Defence = 2,
		Skill = 3,
	}

	public enum ItemType : int {
		Weapon1					= 1,
		Weapon2					= 2,
		Head					= 3,
		Body					= 4,
		Leg						= 5,
		Glove					= 6,
		Boots					= 7,
		Necklace				= 8,
		Earing					= 9,
		Ring					= 10,
		DrugAndSroll			= 11,	// 药水/卷轴
		Gem						= 12,	// 宝玉
		Other					= 15,	// 其他
		Weapon1Cash				= 16,	// 点券主武器
		Weapon2Cash				= 17,	// 点券辅助武器
		HeadCash				= 18,	// 点券帽子
		BodyCash				= 19,	// 点券上装
		LegCash					= 20,	// 点券下装
		GloveCash				= 21,	// 点券手套
		BootsCash				= 22,	// 点券鞋子
		NecklaceCash			= 23,	// 点券项链
		EaringCash				= 24,	// 点券耳环
		RingCash				= 25,	// 点券戒指
		WingCash				= 26,	// 翅膀
		TailCash				= 27,	// 尾巴
		StampCash				= 28,	// 花纹
		FairyCash				= 29,	// 精灵
		DrugAndScrollCash		= 30,	// 药水卷轴
		EthantCash				= 31,	
		OtherCash				= 32,	// 其他点券类物品
		Glyph					= 33,	// 普通强化纹章
		SkillGlyph				= 34,	// 技能纹章
		SpecialSkillGlypgh		= 35,	// 特殊技能纹章

		CookBook				= 39,	// 料理书
		Food					= 40,	// 食物
		Seed					= 41,	// 种子
		FarmingProduct			= 42,	// 农产品
		FishProduct				= 43,	// 海产品

		Vehicle					= 47,	// 坐骑
		ChangeFaceCash			= 48,	
		DragonMoneyCash			= 49,	// 龙币

		Source					= 51,	// 根源
		Tailsman				= 52,	// 护符

		DragonJewel				= 54,	// 龙玉
		SkillDragonJewel		= 55,

		Coupon					= 64,	// 交换券
		SealItem				= 65,	// 封印印章
		EquipmentMaterial		= 66,		// 装备材料
		DraonJewelMaterial		= 67,		// 龙玉材料
		TailsmanMaterial		= 68,		// 纹章材料
		SkillJewelMaterial		= 69,		// 技能龙玉材料

		Mercenary				= 74,		// 佣兵
		MonsterCard				= 75,		// 怪物卡
		Imprinting				= 76,		// 刻印
	}

}
